import java.util.Scanner;
public class ShoutandWhisper {
public static void main(String[] args)
{
//defining the variables
String word, word1;
//Input
Scanner kb = new Scanner(System.in);
System.out.print("Give me a word that has 7 letters > ");
word = kb.nextLine();
word1 = word.toUpperCase();
System.out.println("I shout " + word1 + " when I'm excited, but i whisper " + word + " when i can't disturb the neigbours.");
char achar = word.charAt(0);
char bchar = word.charAt(1);
char cchar = word.charAt(2);
char dchar = word.charAt(3);
char echar = word.charAt(4);
char fchar = word.charAt(5);
char gchar = word.charAt(6);
//Changing to uppercase
char Bchar = Character.toUpperCase(bchar);
char Dchar = Character.toUpperCase(dchar);
char Fchar = Character.toUpperCase(fchar);
System.out.println("And the mixed up word is " + achar+Bchar+cchar+Dchar+echar+Fchar+gchar);
}
}
Now the code does work but is there a shorter way to produce alternating Uppercase or Lowercase outputs without using a For counter? rewriting the entire "newer" variables is an accident waiting to happen. Especially with me.
I tried to force it as i was selecting the letter by letter and I couldn't do it.
Main goal is to either Shout or whisper and then sort the word using alternating Uppercases from second letter.
Alexis C. :
You can convert the word to an array of char and use a for loop to alternate upper and lower case :\n\nchar [] c = word.toLowerCase().toCharArray();\n\nfor(int i = 1; i < c.length; i = i+2){\n c[i] = Character.toUpperCase(c[i]);\n}\n\n\nThen just construct a new String with this char array (using the constructor String(char[] value)) and you'll get your word.",
2013-11-16T22:16:07